﻿#include "src/wave/base_wave_widget.h"
#include <QKeyEvent>
#include <QMenu>
#include <QAction>
#include <QContextMenuEvent>
#include <QColorDialog>
#include <QColor>

BaseWaveWidget::BaseWaveWidget(QWidget *parent)
  : QCustomPlot(parent)
{
  // 设置X轴
  xAxis->setVisible(true); // 显示下方x轴
  xAxis->ticker()->setTickCount(5); // 设置刻度为5格
  xAxis->setRange(0, 1);
  xAxis->setLabel(tr("采集点数"));
  QFont label_font = xAxis->tickLabelFont();
  label_font.setPointSize(12);
  xAxis->setTickLabelFont(label_font);

  // 设置Y轴
  yAxis->setVisible(true); // 显示左边y轴
  yAxis->ticker()->setTickCount(5); // 设置刻度为5格
  yAxis->setRange(-1, 1);
  yAxis->setLabel("电压(V)");
  yAxis2->setVisible(true); // 显示右边y轴
  yAxis2->setTickLabels(false); // 设置不显示x轴刻度
  yAxis2->ticker()->setTickCount(5); // 设置刻度为5格
  yAxis2->setRange(-1, 1);

  legend->setVisible(true); // 设置图例可见
  legend->setBrush(QColor(255, 255, 255, 0)); // 图例背景透明

  setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

  replot(QCustomPlot::rpQueuedReplot);
}

void BaseWaveWidget::InitChannel(int channel_number, const QVector<QColor> &channel_color)
{
  channel_color_ = channel_color;
  channel_number_ = channel_number;
  for (int var = 0; var < channel_number_; ++var)
  {
    addGraph();
    graph(var)->setPen(QPen(channel_color_.at(var), 1));
    graph(var)->setName("AIN " + QString::number(var + 1));
  }
  replot();
}

void BaseWaveWidget::AddData(const int& channel, const double& temp_data) {
  if (channel >= channel_number_ || channel < 0) {
    return;
  }

  // 更新y轴最大值
  if (temp_data > y_max_value_) {
    y_max_value_ = temp_data;
  }

  // 更新y轴最小值
  if (temp_data < y_min_value_) {
    y_min_value_ = temp_data;
  }

  auto data_index = graph(channel)->data().data()->size();
  graph(channel)->addData(data_index, temp_data);
}

void BaseWaveWidget::AddData(const int& channel, const double& key, const double& temp_data) {
  if (channel >= channel_number_ || channel < 0) {
    return;
  }

  // 更新y轴最大值
  if (temp_data > y_max_value_) {
    y_max_value_ = temp_data;
  }

  // 更新y轴最小值
  if (temp_data < y_min_value_) {
    y_min_value_ = temp_data;
  }

  graph(channel)->addData(key, temp_data);
}

void BaseWaveWidget::Clear() {
  for (int var = 0; var < channel_number_; ++var) {
    graph(var)->data().data()->clear();
  }

  y_max_value_ = INT_MIN;
  y_min_value_ = INT_MAX;
}

void BaseWaveWidget::Clear(int channel) {
  if (channel >= channel_number_ || channel < 0) {
    return;
  }

  graph(channel)->data().data()->clear();
}

uint32_t BaseWaveWidget::FindMaxDataLength() {
  uint32_t data_length_max = 0;
  for (int i = 0; i < channel_number_; ++i) {
    data_length_max = std::max(graph(i)->data()->size(), int(data_length_max));
  }
  return data_length_max;
}

void BaseWaveWidget::AutoScale_Y()
{
  double max = y_max_value();
  double min = y_min_value();

  if(min >= 0 && max >= 0){
    max *= 1.1;
    min *= 0.9;
  }else if(min < 0 && max < 0){
    max *= 0.9;
    min *= 1.1;
  }else{
    max *= 1.1;
    min *= 1.1;
  }

  yAxis->setRange(min, max);
}


double BaseWaveWidget::y_max_value() const
{
  if (y_max_value_ != INT_MIN) {
    return y_max_value_;
  }
  return 1;
}

double BaseWaveWidget::y_min_value() const
{
  if (y_min_value_ != INT_MAX){
    return y_min_value_;
  }
  return -1;
}
